package cn.rengy.web.framework.wxmp.message;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rengy.lang.ResultEntity;
import cn.rengy.tpa.wxmp.message.CustomerMsg;
import cn.rengy.tpa.wxmp.token.AccessToken;
import cn.rengy.web.framework.sql.DbService;
import cn.rengy.web.framework.wxmp.token.AccessTokenCacheService;

@Controller
@RequestMapping(value = "/authenticated/wxmsg")
public class WxMessageController {
	private static Logger logger = LoggerFactory.getLogger(WxMessageController.class);
	@Autowired
	private DbService dbService;
	@Autowired
	private AccessTokenCacheService accessTokenCacheService;
	/**
	 * 发送客服消息
	 * @param paramMap
	 * @return	
	 * @throws Exception 
	 */
	@RequestMapping(value="/customerMsg",method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<?> customerMsg (@RequestBody(required=false) Map<String,Object> paramMap) throws Exception{
		String openid=(String)paramMap.get("openid");
		Map<String,Object> user=this.dbService.queryForMap("select_get_wx_user_byid", openid);
		String appid=(String)user.get("appid");
		String content=(String)paramMap.get("content");
		AccessToken accessToken=accessTokenCacheService.getAccessToken(appid);
		new CustomerMsg(accessToken).sendText(content,openid);
		return ResultEntity.ok();
	}
}
